package com.example.a2306_fx.fg

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.CartEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CartViewModel:BaseViewModel() {
    //请求列表
    val cartSuccess = MutableLiveData<Res<CartEntity>>()
    val cartFail = MutableLiveData<String>()
    fun cart(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                cartSuccess.postValue(apiService.cart(isValid, limit, page))
            } catch (e: Exception) {
                cartFail.postValue(e.message)
            }
        }
    }
    //修改购物车
    val updateSuccess = MutableLiveData<Res<String?>>()
    val updateFail = MutableLiveData<String>()
    fun cartUpdate(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.postValue(apiService.cartUpdate(id, number))
            } catch (e: Exception) {
                updateFail.postValue(e.message)
            }
        }
    }
    //删除购物车
    val deleteSuccess = MutableLiveData<Res<String?>>()
    val deleteFail = MutableLiveData<String>()
    fun cartDelete(ids:MutableList<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.postValue(apiService.cartDelete(ids))
            } catch (e: Exception) {
                deleteFail.postValue(e.message.toString())
            }
        }
    }
}